package com.enjoyor.health.HGPT.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.enjoyor.health.HGPT.core.entity.UserInfo;
import com.enjoyor.health.HGPT.core.entity.UserInfoMiniapp;
import com.enjoyor.health.HGPT.core.mapper.UserInfoMapper;
import com.enjoyor.health.HGPT.core.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 体检报告用户表 服务实现类
 * </p>
 *
 * @author enjoyor
 * @since 2023-10-18
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public void insertOrUpdateUserInfo(UserInfo userInfo) {
        String dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        Map<String, Object> map = BeanUtil.beanToMap(userInfo);

        map.put("gender", userInfo.getGender());
        map.put("age", userInfo.getAge()==null? null : (int)userInfo.getAge());
        map.put("id_number", userInfo.getIdNumber());
        map.put("storage_time", dateTime);
        map.put("update_time", dateTime);
        map.put("reg_time", dateTime);
        userInfoMapper.insertOrUpdateUserInfo(map);
    }

    @Override
    public UserInfo getUserInfoFromOpenId(String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("open_id", openId);
        return userInfoMapper.getUserInfoFromOpenId(map);
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {
        LambdaUpdateWrapper<UserInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(UserInfo::getId, userInfo.getId())
                //.set(StringUtils.isNotBlank(userInfo.getName()), UserInfo::getName, userInfo.getName())
                .set(userInfo.getGender() != null, UserInfo::getGender, userInfo.getGender())
                .set(StringUtils.isNotBlank(userInfo.getIdNumber()), UserInfo::getIdNumber, userInfo.getIdNumber())
                .set(StringUtils.isNotBlank(userInfo.getHeight()), UserInfo::getHeight, userInfo.getHeight())
                .set(StringUtils.isNotBlank(userInfo.getWeight()), UserInfo::getWeight, userInfo.getWeight())
                .set(UserInfo::getUpdateTime, userInfo.getUpdateTime());

        return userInfoMapper.update(null, lambdaUpdateWrapper);
    }

    @Override
    public List<UserInfo> getUserInfoByPhone(String phone) {
        QueryWrapper<UserInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("phone", phone);
        return baseMapper.selectList(queryWrapper);
    }
}
